#!/usr/bin/env python
# coding=utf-8
"""
@Author  : youjia - 卞志伟
@file    : 测试数据库方法.py
@contact : bianzhiwei@iyoujia.com
@time    : 2019-09-09 15:51 
@Desc    : 
@Software: PyCharm
"""
import os
import sys

# 当前文件的路径
pwd, filename = os.path.split(os.path.abspath(__file__))
# 当前文件的父路径
father_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + ".")
# 当前文件的前两级目录
grader_father = os.path.abspath(os.path.dirname(pwd) + os.path.sep + "..")
sys.path.append(pwd)
sys.path.append(father_path)
sys.path.append(grader_father)

import json
import logging
import records
import pymongo
import pandas as pd
import pymysql
from sqlalchemy import create_engine

# youjia_13_jingdui_db = create_engine("mysql+pymysql://jingdui:Iudng&6dks@172.16.10.13/youjia_report?charset=utf8")
# youjia_13_sjz_db = create_engine("mysql+pymysql://data_sjz:W0rVo6Kg87JOnRAr@172.16.10.13/youjia?charset=utf8")
# youjia_26_db = create_engine("mysql+pymysql://bianzhiwei:yeO4FKAIleCBiIZ5@192.168.80.26/youjia_report?charset=utf8")
#
# db_youjia_url = 'mysql+pymysql://data_sjz:W0rVo6Kg87JOnRAr@172.16.10.13/youjia'
#
con_youjia = pymysql.connect(host='172.16.10.13', port=3306, user='data_sjz', password='W0rVo6Kg87JOnRAr', db='youjia',
                             charset='utf8')


# con_tpp = pymysql.connect(host='172.16.10.13', port=3306, user='data_sjz', password='W0rVo6Kg87JOnRAr', db='youjia_tpp',
#                           charset='utf8')
# con_report = pymysql.connect(host='172.16.10.13', port=3306, user='data_sjz', password='W0rVo6Kg87JOnRAr',
#                              db='youjia_report', charset='utf8')
# import MySQLdb


def youjia_tongji(func):
    def conn(*args, **kwargs):
        db_name, db_pwd = 'data_sjz', 'W0rVo6Kg87JOnRAr'
        db_host, db_table = '172.16.10.13', 'youjia_report'

        conn = records.Database('mysql+pymysql://%s:%s@%s/%s?charset=utf8' % (db_name, db_pwd, db_host, db_table))
        # conn = MySQLdb.connect(host='172.16.10.13', port=3306, user='data_sjz', passwd='W0rVo6Kg87JOnRAr',
        #                        db='youjia_report', charset='utf8')
        db = conn.cursor()
        result = func(db, *args, **kwargs)
        conn.commit()
        conn.close()
        return result

    return conn


#  with get_connection() as conn:
#         try:
#             df = conn.query(sql).export("df")
class DataSource(object):
    _data_source = {}

    def house(self, func):
        _data_source = self._data_source

        def _data_frame(*args, **kwargs):
            if "house" not in _data_source:
                sql = "select * from youjia.lod_house"
                _data_source['house'] = pd.read_sql_query(sql, con_youjia)
                result = func(_data_source['house'], *args, **kwargs)
                return result

        self._data_source = _data_source
        return _data_frame


def youjia13(func):
    _instance = {}

    def _data_frame(*args, **kwargs):
        sql = "select * from youjia.lod_house"
        if "df" not in _instance:
            _instance['df'] = pd.read_sql_query(sql, con_youjia)
        result = func(_instance['df'], *args, **kwargs)
        return result

    return _data_frame


@DataSource
def save(df):
    print(df.head(1))

#
# @youjia13
# def pa(df):
#     print(df.head(1))
#
#
# @youjia13
# def aa(df):
#     print(df.head(1))
#
#
# @youjia13
# def bb(df):
#     print(df.head(1))


if __name__ == '__main__':
    save()
    # pa()
    # aa()
    # bb()
